{
 "cells": [
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-20T07:33:44.233333Z",
     "start_time": "2025-04-20T07:33:39.594100Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 初始化模拟的设备配置信息\n",
    "from langchain_chroma import Chroma\n",
    "from langchain_openai import OpenAIEmbeddings\n",
    "import os\n",
    "# 初始化 OpenAI Embeddings 或其他嵌入函数\n",
    "embeddings = OpenAIEmbeddings()\n",
    "BASE_DIR = os.getcwd()\n",
    "print(BASE_DIR)\n",
    "persist_directory = rf\"{BASE_DIR}\\common\\VectorStore\"\n",
    "print(persist_directory)\n",
    "# 创建 Chroma 对象\n",
    "vector_store = Chroma(\n",
    "    collection_name=\"vector_collection_for_agent\",\n",
    "    embedding_function=embeddings,\n",
    "    persist_directory=persist_directory,\n",
    "    collection_metadata={\n",
    "        \"vs_name\":\"test\"\n",
    "    }\n",
    ")"
   ],
   "id": "f613319c9d2df820",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "D:\\DevelopFiles\\AIGC\\HomeAutoAgent\n",
      "D:\\DevelopFiles\\AIGC\\HomeAutoAgent\\common\\VectorStore\n"
     ]
    }
   ],
   "execution_count": 1
  },
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-04-20T07:36:17.148369Z",
     "start_time": "2025-04-20T07:36:15.807327Z"
    }
   },
   "source": [
    "import os\n",
    "from langchain_core.documents import Document\n",
    "import json\n",
    "BASE_DIR = os.getcwd()\n",
    "# 文件路径\n",
    "file_path = rf\"{BASE_DIR}\\oneNetConfig.json\"\n",
    "\n",
    "\n",
    "\n",
    "# 打开并读取 JSON 文件内容\n",
    "with open(file_path, 'r', encoding='utf-8') as file:\n",
    "    data = json.load(file)\n",
    "data_documents:list[Document]=[]\n",
    "# 遍历每个设备的 JSON 对象并打印\n",
    "for device in data:\n",
    "    dict_str=json.dumps(device, indent=2, ensure_ascii=False)\n",
    "    \n",
    "    # print(type(dict_str))\n",
    "    data_documents.append(Document(page_content=dict_str))\n",
    "ids=vector_store.add_documents(data_documents)\n",
    "print(ids)"
   ],
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['49445fd6-8331-4ac3-9fe2-d0d931a6375b', '68dadea2-e51e-491e-a80a-f62dbbd618c6', '0c7bb8f4-5155-45a1-b07f-12b006bbd1ef', 'a6643775-ec34-4049-8693-739eaf62c35f', '4891288f-b9dc-4fe9-8b61-1250ba0b37fe', '920da463-0a10-423f-91bc-d98e37029922', '90f5e92f-0c2c-4b08-811a-a01fe95a9d18', '051b828b-0130-4208-9d5a-d8531a250c00', 'f8cad18e-63fb-4760-af79-b8061e7b2474']\n"
     ]
    }
   ],
   "execution_count": 5
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-20T07:36:18.470916Z",
     "start_time": "2025-04-20T07:36:18.463696Z"
    }
   },
   "cell_type": "code",
   "source": [
    "from langchain_core.documents import Document\n",
    "all=vector_store.get(\n",
    "    # ids=['688875e8-b0d9-457f-ac82-68a78a4a06b4', '6803132b-29fb-46d3-9b03-7fde31994fc1', '3571348a-0d14-4231-abfa-a9684299c9d0', '4e812df8-bdd6-4371-8fed-664f231732ad', 'c7425539-357a-42b0-a6a5-0ad8bdfddccd', '1275019f-cbd4-476a-b453-df3a3b4570a3', '8415ad06-9114-4405-a222-5cc09a302451', '2bdf1514-9a6c-4663-a9f0-24a763c5cb78', '8b87e417-222e-43d9-a2f8-065e23005cfe']\n",
    ")\n",
    "# print(all)\n",
    "for i in all[\"documents\"]:\n",
    "    doc=Document(page_content=i)\n",
    "    print(\"=\"*8)\n",
    "    print(doc)"
   ],
   "id": "a762a6e99cc84dde",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "========\n",
      "page_content='{\n",
      "  \"product_id\": {\n",
      "    \"type\": \"string\",\n",
      "    \"description\": \"产品ID\",\n",
      "    \"value\": \"mD97Vya1hi\"\n",
      "  },\n",
      "  \"device_name\": {\n",
      "    \"type\": \"string\",\n",
      "    \"description\": \"设备名称\",\n",
      "    \"value\": \"d1\"\n",
      "  },\n",
      "  \"device_type\": \"LED Light\",\n",
      "  \"params\": {\n",
      "    \"type\": \"object\",\n",
      "    \"description\": \"控制参数，字典格式\",\n",
      "    \"properties\": {\n",
      "      \"led\": {\n",
      "        \"type\": \"boolean\",\n",
      "        \"description\": \"LED开关状态\",\n",
      "        \"value_range\": \"true/false\"\n",
      "      }\n",
      "    }\n",
      "  }\n",
      "}'\n",
      "========\n",
      "page_content='{\n",
      "  \"product_id\": {\n",
      "    \"type\": \"string\",\n",
      "    \"description\": \"产品ID\",\n",
      "    \"value\": \"aX23Jrf5xy\"\n",
      "  },\n",
      "  \"device_name\": {\n",
      "    \"type\": \"string\",\n",
      "    \"description\": \"设备名称\",\n",
      "    \"value\": \"空调\"\n",
      "  },\n",
      "  \"device_type\": \"Air Conditioner\",\n",
      "  \"params\": {\n",
      "    \"type\": \"object\",\n",
      "    \"description\": \"控制参数，字典格式\",\n",
      "    \"properties\": {\n",
      "      \"power\": {\n",
      "        \"type\": \"boolean\",\n",
      "        \"description\": \"开关状态\",\n",
      "        \"value_range\": \"true/false\"\n",
      "      },\n",
      "      \"temperature\": {\n",
      "        \"type\": \"integer\",\n",
      "        \"description\": \"设定温度\",\n",
      "        \"value_range\": \"16-30\"\n",
      "      },\n",
      "      \"fan_speed\": {\n",
      "        \"type\": \"integer\",\n",
      "        \"description\": \"风速级别\",\n",
      "        \"value_range\": \"1-5\"\n",
      "      },\n",
      "      \"mode\": {\n",
      "        \"type\": \"string\",\n",
      "        \"description\": \"空调模式\",\n",
      "        \"value_range\": \"冷/热/除湿/自动\"\n",
      "      }\n",
      "    }\n",
      "  }\n",
      "}'\n",
      "========\n",
      "page_content='{\n",
      "  \"product_id\": {\n",
      "    \"type\": \"string\",\n",
      "    \"description\": \"产品ID\",\n",
      "    \"value\": \"pR65Xtq3lc\"\n",
      "  },\n",
      "  \"device_name\": {\n",
      "    \"type\": \"string\",\n",
      "    \"description\": \"设备名称\",\n",
      "    \"value\": \"风扇\"\n",
      "  },\n",
      "  \"device_type\": \"Electric Fan\",\n",
      "  \"params\": {\n",
      "    \"type\": \"object\",\n",
      "    \"description\": \"控制参数，字典格式\",\n",
      "    \"properties\": {\n",
      "      \"power\": {\n",
      "        \"type\": \"boolean\",\n",
      "        \"description\": \"开关状态\",\n",
      "        \"value_range\": \"true/false\"\n",
      "      },\n",
      "      \"fan_speed\": {\n",
      "        \"type\": \"integer\",\n",
      "        \"description\": \"风速级别\",\n",
      "        \"value_range\": \"1-5\"\n",
      "      },\n",
      "      \"oscillation\": {\n",
      "        \"type\": \"boolean\",\n",
      "        \"description\": \"是否开启摇头\",\n",
      "        \"value_range\": \"true/false\"\n",
      "      }\n",
      "    }\n",
      "  }\n",
      "}'\n",
      "========\n",
      "page_content='{\n",
      "  \"product_id\": {\n",
      "    \"type\": \"string\",\n",
      "    \"description\": \"产品ID\",\n",
      "    \"value\": \"zD81Vyr8jh\"\n",
      "  },\n",
      "  \"device_name\": {\n",
      "    \"type\": \"string\",\n",
      "    \"description\": \"设备名称\",\n",
      "    \"value\": \"加湿器\"\n",
      "  },\n",
      "  \"device_type\": \"Humidifier\",\n",
      "  \"params\": {\n",
      "    \"type\": \"object\",\n",
      "    \"description\": \"控制参数，字典格式\",\n",
      "    \"properties\": {\n",
      "      \"power\": {\n",
      "        \"type\": \"boolean\",\n",
      "        \"description\": \"开关状态\",\n",
      "        \"value_range\": \"true/false\"\n",
      "      },\n",
      "      \"humidity_level\": {\n",
      "        \"type\": \"integer\",\n",
      "        \"description\": \"设定湿度\",\n",
      "        \"value_range\": \"30-80\"\n",
      "      },\n",
      "      \"mist_output\": {\n",
      "        \"type\": \"integer\",\n",
      "        \"description\": \"雾量大小\",\n",
      "        \"value_range\": \"1-3\"\n",
      "      }\n",
      "    }\n",
      "  }\n",
      "}'\n",
      "========\n",
      "page_content='{\n",
      "  \"product_id\": {\n",
      "    \"type\": \"string\",\n",
      "    \"description\": \"产品ID\",\n",
      "    \"value\": \"vC38Ltz9mn\"\n",
      "  },\n",
      "  \"device_name\": {\n",
      "    \"type\": \"string\",\n",
      "    \"description\": \"设备名称\",\n",
      "    \"value\": \"音箱\"\n",
      "  },\n",
      "  \"device_type\": \"Speaker\",\n",
      "  \"params\": {\n",
      "    \"type\": \"object\",\n",
      "    \"description\": \"控制参数，字典格式\",\n",
      "    \"properties\": {\n",
      "      \"power\": {\n",
      "        \"type\": \"boolean\",\n",
      "        \"description\": \"开关状态\",\n",
      "        \"value_range\": \"true/false\"\n",
      "      },\n",
      "      \"volume\": {\n",
      "        \"type\": \"integer\",\n",
      "        \"description\": \"音量级别\",\n",
      "        \"value_range\": \"0-100\"\n",
      "      },\n",
      "      \"sound_mode\": {\n",
      "        \"type\": \"string\",\n",
      "        \"description\": \"音效模式\",\n",
      "        \"value_range\": \"音乐/电影/游戏/新闻\"\n",
      "      }\n",
      "    }\n",
      "  }\n",
      "}'\n",
      "========\n",
      "page_content='{\n",
      "  \"product_id\": {\n",
      "    \"type\": \"string\",\n",
      "    \"description\": \"产品ID\",\n",
      "    \"value\": \"xE74Jql1pv\"\n",
      "  },\n",
      "  \"device_name\": {\n",
      "    \"type\": \"string\",\n",
      "    \"description\": \"设备名称\",\n",
      "    \"value\": \"电池\"\n",
      "  },\n",
      "  \"device_type\": \"Smart Battery\",\n",
      "  \"params\": {\n",
      "    \"type\": \"object\",\n",
      "    \"description\": \"控制参数，字典格式\",\n",
      "    \"properties\": {\n",
      "      \"battery_display\": {\n",
      "        \"type\": \"boolean\",\n",
      "        \"description\": \"是否显示电量\",\n",
      "        \"value_range\": \"true/false\"\n",
      "      },\n",
      "      \"charging\": {\n",
      "        \"type\": \"boolean\",\n",
      "        \"description\": \"是否正在充电\",\n",
      "        \"value_range\": \"true/false\"\n",
      "      },\n",
      "      \"power_saving\": {\n",
      "        \"type\": \"boolean\",\n",
      "        \"description\": \"是否开启省电模式\",\n",
      "        \"value_range\": \"true/false\"\n",
      "      }\n",
      "    }\n",
      "  }\n",
      "}'\n",
      "========\n",
      "page_content='{\n",
      "  \"product_id\": {\n",
      "    \"type\": \"string\",\n",
      "    \"description\": \"产品ID\",\n",
      "    \"value\": \"cD89Tgh2xz\"\n",
      "  },\n",
      "  \"device_name\": {\n",
      "    \"type\": \"string\",\n",
      "    \"description\": \"设备名称\",\n",
      "    \"value\": \"电视\"\n",
      "  },\n",
      "  \"device_type\": \"Smart TV\",\n",
      "  \"params\": {\n",
      "    \"type\": \"object\",\n",
      "    \"description\": \"控制参数，字典格式\",\n",
      "    \"properties\": {\n",
      "      \"power\": {\n",
      "        \"type\": \"boolean\",\n",
      "        \"description\": \"开关状态\",\n",
      "        \"value_range\": \"true/false\"\n",
      "      },\n",
      "      \"volume\": {\n",
      "        \"type\": \"integer\",\n",
      "        \"description\": \"音量级别\",\n",
      "        \"value_range\": \"0-100\"\n",
      "      },\n",
      "      \"channel\": {\n",
      "        \"type\": \"integer\",\n",
      "        \"description\": \"电视频道号\",\n",
      "        \"value_range\": \"1-999\"\n",
      "      },\n",
      "      \"picture_mode\": {\n",
      "        \"type\": \"string\",\n",
      "        \"description\": \"画质模式\",\n",
      "        \"value_range\": \"标准/动态/电影/自定义\"\n",
      "      }\n",
      "    }\n",
      "  }\n",
      "}'\n",
      "========\n",
      "page_content='{\n",
      "  \"product_id\": {\n",
      "    \"type\": \"string\",\n",
      "    \"description\": \"产品ID\",\n",
      "    \"value\": \"wJ12Xtm6yb\"\n",
      "  },\n",
      "  \"device_name\": {\n",
      "    \"type\": \"string\",\n",
      "    \"description\": \"设备名称\",\n",
      "    \"value\": \"窗帘\"\n",
      "  },\n",
      "  \"device_type\": \"Smart Curtain\",\n",
      "  \"params\": {\n",
      "    \"type\": \"object\",\n",
      "    \"description\": \"控制参数，字典格式\",\n",
      "    \"properties\": {\n",
      "      \"open_close\": {\n",
      "        \"type\": \"boolean\",\n",
      "        \"description\": \"窗帘开合状态\",\n",
      "        \"value_range\": \"true/false\"\n",
      "      },\n",
      "      \"position\": {\n",
      "        \"type\": \"integer\",\n",
      "        \"description\": \"窗帘位置百分比\",\n",
      "        \"value_range\": \"0-100\"\n",
      "      },\n",
      "      \"auto_mode\": {\n",
      "        \"type\": \"boolean\",\n",
      "        \"description\": \"是否开启自动模式\",\n",
      "        \"value_range\": \"true/false\"\n",
      "      }\n",
      "    }\n",
      "  }\n",
      "}'\n",
      "========\n",
      "page_content='{\n",
      "  \"product_id\": {\n",
      "    \"type\": \"string\",\n",
      "    \"description\": \"产品ID\",\n",
      "    \"value\": \"mN41Kyq9zf\"\n",
      "  },\n",
      "  \"device_name\": {\n",
      "    \"type\": \"string\",\n",
      "    \"description\": \"设备名称\",\n",
      "    \"value\": \"门锁\"\n",
      "  },\n",
      "  \"device_type\": \"Smart Lock\",\n",
      "  \"params\": {\n",
      "    \"type\": \"object\",\n",
      "    \"description\": \"控制参数，字典格式\",\n",
      "    \"properties\": {\n",
      "      \"lock\": {\n",
      "        \"type\": \"boolean\",\n",
      "        \"description\": \"是否上锁\",\n",
      "        \"value_range\": \"true/false\"\n",
      "      },\n",
      "      \"fingerprint_unlock\": {\n",
      "        \"type\": \"boolean\",\n",
      "        \"description\": \"是否启用指纹开锁\",\n",
      "        \"value_range\": \"true/false\"\n",
      "      },\n",
      "      \"alarm\": {\n",
      "        \"type\": \"boolean\",\n",
      "        \"description\": \"是否启用防盗警报\",\n",
      "        \"value_range\": \"true/false\"\n",
      "      }\n",
      "    }\n",
      "  }\n",
      "}'\n"
     ]
    }
   ],
   "execution_count": 6
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-20T07:36:11.738683Z",
     "start_time": "2025-04-20T07:36:11.682062Z"
    }
   },
   "cell_type": "code",
   "source": [
    "\n",
    "ids=vector_store.get()['ids']\n",
    "vector_store.delete(ids=ids)"
   ],
   "id": "8d41b6e0f77ef097",
   "outputs": [],
   "execution_count": 4
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-20T08:43:05.305661Z",
     "start_time": "2025-04-20T08:43:01.815548Z"
    }
   },
   "cell_type": "code",
   "source": [
    "from langchain_core.messages import HumanMessage\n",
    "from common.structs import DeviceModelFactory, DeviceCalls\n",
    "from langchain_openai import ChatOpenAI\n",
    "from langchain_core.documents import Document\n",
    "\n",
    "configs=[Document(page_content=\"\"\"{\n",
    "  \"product_id\": {\n",
    "    \"type\": \"string\",\n",
    "    \"description\": \"产品ID\",\n",
    "    \"value\": \"mD97Vya1hi\"\n",
    "  },\n",
    "  \"device_name\": {\n",
    "    \"type\": \"string\",\n",
    "    \"description\": \"设备名称\",\n",
    "    \"value\": \"d1\"\n",
    "  },\n",
    "  \"device_type\": \"LED Light\",\n",
    "  \"params\": {\n",
    "    \"type\": \"object\",\n",
    "    \"description\": \"控制参数，字典格式\",\n",
    "    \"properties\": {\n",
    "      \"led\": {\n",
    "        \"type\": \"boolean\",\n",
    "        \"description\": \"LED开关状态\",\n",
    "        \"value_range\": \"true/false\"\n",
    "      }\n",
    "    }\n",
    "  }\n",
    "}\"\"\")]\n",
    "\n",
    "factory = DeviceModelFactory()\n",
    "factory.generate_all(configs)\n",
    "ConfigUnion = factory.get_union_type()\n",
    "DeviceCallsDynamic = DeviceCalls[ConfigUnion]\n",
    "DeviceCallsDynamic.__name__ = \"DeviceCallsDynamic\"\n",
    "print(f\"{DeviceCallsDynamic.__name__}+++++++++++++++++++++++++++++++++++++++++++++++++\")\n",
    "\n",
    "llm=ChatOpenAI(\n",
    "    model=\"gpt-4o-mini\"\n",
    ")\n",
    "llm=llm.with_structured_output(DeviceCallsDynamic)\n",
    "\n",
    "\n",
    "llm.invoke([HumanMessage(content=\"请控制LED灯打开，参数随便\")])"
   ],
   "id": "ba02395693f182b5",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "model_name:LEDLightConfig\n",
      "DeviceCallsDynamic+++++++++++++++++++++++++++++++++++++++++++++++++\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "DeviceCallsDynamic(device_calls=[DeviceCall[LEDLightConfig](device_name='Living Room LED', device_id='led001', config=LEDLightConfig(led=True), order=1)])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 7
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
